<template>
  <div>
    <Card shadow>
      <p slot="title">
        快捷查询
      </p>
      <Form :label-width="120">
        <Row>
          <Col span="4">
            <FormItem label="医生：">
              <Select v-model="condition.doctorId" clearable filterable style="width: 100%">
                <Option v-for="item in doctors" :value="item.id" :key="item.id">{{ item.username }}</Option>
              </Select>
            </FormItem>
          </Col>
          <Col span="4">
            <FormItem label="护士：">
              <Select v-model="condition.nurseId" clearable filterable style="width: 100%">
                <Option v-for="item in nurses" :value="item.id" :key="item.id">{{ item.username }}</Option>
              </Select>
            </FormItem>
          </Col>
          <Col span="4">
            <FormItem label="预约时间：">
              <DatePicker v-model="condition.appointment" placeholder="请选择" format="yyyy-MM-dd" type="date" style="width: 100%"></DatePicker>
            </FormItem>
          </Col>
          <Col span="4">
            <FormItem label="处理方式：">
              <Select v-model="condition.process" clearable filterable style="width: 100%">
                <Option :value="1" :key="1">电话随访</Option>
                <Option :value="2" :key="2">预约到店</Option>
              </Select>
            </FormItem>
          </Col>
          <Col span="4">
            <FormItem label="预约类型：">
              <Select v-model="condition.type" clearable filterable style="width: 100%">
                <Option :value="1" :key="1">会员预约</Option>
                <Option :value="2" :key="2">非会员预约</Option>
              </Select>
            </FormItem>
          </Col>
        </Row>
      </Form>
    </Card>
    <Card style="margin-top: 5px" shadow>
        <iview-table
            ref="table"
            :span1="20"
            :span2="4"
            :baseURL="queryModel.baseURL"
            :menuId="dataModel.menuId"
            :columns="dataModel.columns"
            :order="dataModel.order"
            :pageNumber="pageModel.pageNumber"
            :pageSize="pageModel.pageSize"
            :defaultFilters="queryModel.defaultFilters"
            v-on="{changeSelect:changeSelect,changeTotalSize:changeTotalSize,events:events}">
        </iview-table>
        <br>
        <iview-page
            :totalSize="pageModel.totalSize"
            :pageNumber="pageModel.pageNumber"
            :pageSize="pageModel.pageSize"
            :pageSizeOpts="pageModel.pageSizeOpts"
            v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
        </iview-page>
    </Card>
  </div>
</template>

<script>
import iviewTable from '@/view/common/table/Table.vue';
import iviewPage from '@/view/common/page/Page.vue';
import expandRow from './ReservationExpand.vue';
export default {
    components: {
        iviewTable,
        iviewPage,
        expandRow
    },
    data () {
        return {
            baseURL: '/user/reservation',
            // 考勤
            attendanceURL: '/user/attendance',
            selectRows: [],
            selectRow: {},
            healthcare: [],
            doctors: [],
            nurses: [],
            showAppointment: false,
            queryModel: {
                // 网关代理路径(必须)+资源路径(可选)
                baseURL: '/user/reservation',
                defaultFilters: [
                    {'field': 'record_type', 'value1': 1, 'operate': '=='}
                ]
            },
            dataModel: {
                menuId: 0,
                columns: [
                    {
                        type: 'expand',
                        width: 50,
                        render: (h, params) => {
                            return h(expandRow, {
                                props: {
                                    row: params.row
                                }
                            })
                        }
                    },{
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },{
                        title: '预约患者',
                        key: 'patientName',
                        sortable: true
                    },{
                        title: '预约类型',
                        key: 'type',
                        sortable: true,
                        render: (h, params) => {
                            let temp = params.row.type
                            if (temp === 1) {
                                temp = '会员预约'
                            } else if (temp === 2) {
                                temp = '非会员预约'
                            }
                            return h('span', temp)
                        }
                    },{
                        title: '提醒方式',
                        key: 'remind',
                        sortable: true,
                        render: (h, params) => {
                            let temp = params.row.remind
                            if (temp === 1) {
                                temp = '短信'
                            } else if (temp === 2) {
                                temp = '微信'
                            } else if (temp === 3) {
                                temp = '电话'
                            }
                            return h('span', temp)
                        }
                    },{
                        title: '处理方式',
                        key: 'process',
                        sortable: true,
                        render: (h, params) => {
                            let temp = params.row.process
                            if (temp === 1) {
                                temp = '电话随访'
                            } else if (temp === 2) {
                                temp = '预约到店'
                            }
                            return h('span', temp)
                        }
                    },{
                        title: '支持医生',
                        key: 'doctorName',
                        sortable: true
                    },{
                        title: '支持护士',
                        key: 'nurseName',
                        sortable: true
                    },{
                        title: '预约时间',
                        key: 'appointment',
                        type: 'date',
                        sortable: true
                    },{
                        title: '当前状态',
                        key: 'status',
                        sortable: true,
                        render: (h, params) => {
                            let color = ''
                            let temp = params.row.status
                            if (temp === 0) {
                                temp = '已预约'
                                color = 'green'
                            } else if (temp === 1) {
                                temp = '已回访'
                                color = 'blue'
                            } else if (temp === 2) {
                                temp = '已到场'
                                color = 'yellow'
                            } else if (temp === 3) {
                                temp = '已接诊'
                                color = 'red'
                            } else if (temp === 4) {
                                temp = '已完成'
                                color = '#19be6b'
                            } else if (temp === 5) {
                                temp = '已取消'
                                color = '#bbbec4'
                            } else if (temp === 6) {
                                temp = '已支持'
                                color = '#ff9900'
                            }
                            return h('Tag',{
                                props: {
                                    type: 'border',
                                    color: color
                                }
                            }, temp)
                        }
                    }
                ],
                order: 'creation_time desc'
            },
            pageModel: {
                totalSize: 0,
                pageNumber: 1,
                pageSize: 10,
                pageSizeOpts: [10, 50, 100, 500]
            },
            condition: {
                type: '',
                process: '',
                appointment: '',
                doctorId: '',
                nurseId: ''
            }
        }
    },
    methods: {
        loadDoctor () {
            let url = '/user/findByJob/1'
            this.$ajax.get(url).then(r => {
                let {code, data, msg} = r.data
                if (code === 0) {
                    this.doctors = data
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        loadNurse () {
            let url = '/user/findByJob/2'
            this.$ajax.get(url).then(r => {
                let {code, data, msg} = r.data
                if (code === 0) {
                    this.nurses = data
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        events (item) {
            if (item.path === 'change') {
                this.change()
            } else if (item.path === 'delete') {
                this.delete()
            }
        },
        change () {
            if (this.selectRows.length > 0) {

            } else {
                this.$bus.alert('warning', '请至少选择一条记录')
            }
        },
        delete () {
            if (this.selectRows.length > 0) {
                let ids = []
                for (let row of this.selectRows) {
                    ids.push(row.id)
                }
                let url = this.baseURL + '/checkDelete'
                this.$ajax.post(url, ids).then((response) => {
                    let {code, data} = response.data
                    if (code === 0) {
                        if (data.length > 0) {
                            if (data.length === this.selectRows.length) {
                                this.doDelete(data)
                            } else {
                                this.$Modal.confirm({
                                    title: '操作提示',
                                    content: '<p>可以删除'+data.length+'条记录，确认删除?</p>',
                                    onOk: () => {
                                        this.doDelete(data)
                                    }
                                })
                            }
                        } else {
                            this.$bus.alert("warning", "没有符合条件的记录")
                        }
                    }
                })
            } else {
                this.$bus.alert('warning', '请至少选择一条记录')
            }
        },
        doDelete (ids) {
            let url = this.baseURL + '/delete'
            this.$ajax.post(url, ids).then((response) => {
                let {code, msg} = response.data
                if (code === 0) {
                    this.$bus.alert('success', '删除成功')
                    this.refresh()
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        refresh () {
            this.$refs.table.refresh()
        },
        changeSelect (selection) {
            this.selectRows = selection
        },
        changeTotalSize (totalSize) {
            this.pageModel.totalSize = totalSize
        },
        changePageNumber (pageNumber) {
            this.pageModel.pageNumber = pageNumber
        },
        changePageSize (pageSize) {
            this.pageModel.pageSize = pageSize
        },
        changeDoctor (option) {
            this.selectRow.doctorId = option.value
            this.selectRow.doctorName = option.label
        },
        changeNurse (option) {
            this.selectRow.nurseId = option.value
            this.selectRow.nurseName = option.label
        },
        init () {
            this.loadDoctor()
            this.loadNurse()
            this.refresh()
        },
        search () {
            this.pageModel.pageNumber = 1
            this.queryModel.defaultFilters.length = 0
            this.queryModel.defaultFilters.push({'field': 'record_type', 'value1': 1, 'operate': '=='})
            if (this.condition.type && this.condition.type !== '') {
                this.queryModel.defaultFilters.push({'field': 'type', 'value1': this.condition.type, 'operate': '=='})
            }
            if (this.condition.process && this.condition.process !== '') {
                this.queryModel.defaultFilters.push({'field': 'process', 'value1': this.condition.process, 'operate': '=='})
            }
            if (this.condition.doctorId && this.condition.doctorId !== '') {
                this.queryModel.defaultFilters.push({'field': 'doctor_id', 'value1': this.condition.doctorId, 'operate': '=='})
            }
            if (this.condition.nurseId && this.condition.nurseId !== '') {
                this.queryModel.defaultFilters.push({'field': 'nurse_id', 'value1': this.condition.nurseId, 'operate': '=='})
            }
            if (this.condition.appointment && this.condition.appointment !== '') {
                this.queryModel.defaultFilters.push({'field': 'appointment', 'value1': this.$moment(this.condition.appointment).format("YYYY-MM-DD") + ' 00:00:00', 'operate': '>='})
                this.queryModel.defaultFilters.push({'field': 'appointment', 'value1': this.$moment(this.condition.appointment).format("YYYY-MM-DD") + ' 23:59:59', 'operate': '<='})
            }
            this.refresh()
        }
    },
    mounted () {
        this.init()
    },
    watch: {
        '$route' (to) {
            if (to.path === '/support/list') {
                this.init()
            }
        },
        'condition.type': function (val, oldValue) {
            this.search()
        },
        'condition.process': function (val, oldValue) {
            this.search()
        },
        'condition.doctorId': function (val, oldValue) {
            this.search()
        },
        'condition.nurseId': function (val, oldValue) {
            this.search()
        },
        'condition.appointment': function (val, oldValue) {
            this.search()
        }
    }
}
</script>

<style>

</style>
